package com.gexiaobao.pigeon.ui.breeding_pigeon

import android.os.Bundle
import android.view.inputmethod.EditorInfo
import androidx.core.view.isVisible
import androidx.core.widget.addTextChangedListener
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseActivity
import com.gexiaobao.pigeon.app.eventViewModel
import com.gexiaobao.pigeon.app.ext.hideSoftKeyboard
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.ext.initFooter
import com.gexiaobao.pigeon.app.ext.loadListData
import com.gexiaobao.pigeon.app.ext.loadServiceInit
import com.gexiaobao.pigeon.app.ext.showLoading
import com.gexiaobao.pigeon.app.util.StatusBarUtil
import com.gexiaobao.pigeon.app.weight.recycleview.SpaceItemDecoration
import com.gexiaobao.pigeon.databinding.ActivityMakePedigreeBooksBinding
import com.gexiaobao.pigeon.ui.adapter.AdapterMakePedigreeBooks
import com.gexiaobao.pigeon.ui.dialog.RxDialogDefault
import com.gexiaobao.pigeon.viewmodel.BreedingPigeonViewModel
import com.kingja.loadsir.core.LoadService
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat
import me.hgj.jetpackmvvm.util.startActivity

/**
 * @Author      : hxw
 * @Date        : 2024/9/12 16:59
 * @Describe    : 制作血统书
 */
class ActivityMakePedigreeBooks : BaseActivity<BreedingPigeonViewModel, ActivityMakePedigreeBooksBinding>() {

    private val mAdapter: AdapterMakePedigreeBooks by lazy { AdapterMakePedigreeBooks() }
    private lateinit var loadSir: LoadService<Any>

    override fun initView(savedInstanceState: Bundle?) {
        StatusBarUtil.setTranslucentForImageView(this, 0, null)
        mDatabind.includeBar.tvTitle.text = getString(R.string.make_blood_book)

        eventViewModel.toRefreshDataInt.observeInActivity(this) {
            onRefresh(true)
        }

        eventViewModel.toRefreshData.observeInActivity(this) {
            onRefresh(true)
        }

        loadSir = loadServiceInit(mDatabind.includeList.swipeRefreshLayout) {
            onRefresh(true)
        }

        onRefresh(true)

        mDatabind.includeList.swipeRecyclerView.init(LinearLayoutManager(this), mAdapter).let {
            it.addItemDecoration(SpaceItemDecoration(0, 0))
            it.initFooter {
                onRefresh(false)
            }
        }

        mDatabind.includeSearch.editQuery.setOnEditorActionListener { _, i, _ ->
            when (i) {
                EditorInfo.IME_ACTION_SEARCH -> {
                    onRefresh(true)
                    hideSoftKeyboard(this)
                }

                else -> {}
            }
            false
        }

        mDatabind.includeSearch.editQuery.addTextChangedListener {
            mDatabind.includeSearch.ivClose.isVisible = it.toString().isNotEmpty()
        }

        mDatabind.includeList.swipeRefreshLayout.init {
            onRefresh(true)
        }

        mAdapter.addChildClickViewIds(R.id.ivDeleteBloodBook)
        mAdapter.setOnItemChildClickListener { _, view, position ->
            when (view.id) {
                R.id.ivDeleteBloodBook -> showDeleteDialog(mAdapter.data[position].id)
            }
        }

        mAdapter.setOnItemClickListener { _, _, position ->
            startActivity<ActivityMakeBlood>("type" to "edit", "pigeonId" to mAdapter.data[position].id)
        }
    }

    private fun showDeleteDialog(id: Int) {
        val dialog = RxDialogDefault(this)
        dialog.setTitle(getString(R.string.delete_blood_book))
        dialog.setSureListener {
            mViewModel.delBloodBook(id.toString())
            dialog.dismiss()
        }
        dialog.setFullScreenWidth()
        dialog.show()
    }

    private fun onRefresh(isRefresh: Boolean) {
        val query = mDatabind.includeSearch.editQuery.text.toString().trim()
        loadSir.showLoading()
        mViewModel.getBloodBookList(isRefresh, query)
    }

    override fun createObserver() {
        super.createObserver()
        /** 删除血统书 */
        mViewModel.delBloodBookResult.observe(this) {
            onRefresh(true)
        }
        /** 获取血统书列表 */
        mViewModel.getBloodBookResult.observe(this) {
            loadListData(it, mAdapter, loadSir, mDatabind.includeList.swipeRecyclerView, mDatabind.includeList.swipeRefreshLayout)
        }
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(
            mDatabind.includeBar.ivBack,
            mDatabind.includeSearch.tvSearch,
            mDatabind.includeSearch.ivClose,
            mDatabind.btnMakePedigreeBooks
        ) {
            when (it) {
                mDatabind.btnMakePedigreeBooks -> startActivity<ActivityMakeBlood>("type" to "add")
                mDatabind.includeBar.ivBack -> finish()
                mDatabind.includeSearch.tvSearch -> onRefresh(true)
                mDatabind.includeSearch.ivClose -> {
                    mDatabind.includeSearch.editQuery.setText("")
                    onRefresh(true)
                }
            }
        }
    }
}